package networkedmodules;

import java.io.Serializable;

/**
 * An object representation of an update to be sent through
 * a network connection. This is an application of the
 * command pattern. The performUpdate method is invoked at the
 * other end of a connection and should only be called on a
 * Module whose class created it.
 * @author Alex Babis
 */
@SuppressWarnings("serial")
public abstract class Update implements Serializable
{
	private String key;
	
	protected final void performUpdate(ModuleGroup modules)
	{
		Module m = modules.getModule(key);
		// If the group has the module we're looking for,
		// we perform the update on that module. Otherwise,
		// we assume that the module will be added later. We
		// queue the update until this time.
		if(m == null)
		{
			modules.updateQueue.add(this);
			System.out.println(this + " queued");
		}
		else
			performUpdate(modules.getModule(key));
	}
	
	public final void setKey(String key){this.key = key;}
	
	public abstract void performUpdate(Module module);
}
